/*
 * 
 */
package co.edu.udistrital.erpgrid.jaio.commons;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.openswing.swing.server.ConnectionManager;
import org.openswing.swing.server.PoolerConnectionSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Arreglo de el datasource simple de jallinone para satisfacer interceptor
 */
public class SimpleDataSource implements DataSource {
	
	/**
	 * The pw.
	 * @uml.property  name="pw"
	 */
	private PrintWriter pw;
	
	/**
	 * The login timeout.
	 * @uml.property  name="loginTimeout"
	 */
	private int loginTimeout = 1000;
	
	/**
	 * The current conn.
	 * @uml.property  name="currentConn"
	 */
	private Connection currentConn = null;
	
	/**
	 * The opened conns.
	 * @uml.property  name="openedConns"
	 */
	private int openedConns = 0;
	
	/**
	 * Logger.
	 */
	private static final Logger LOG = LoggerFactory
			.getLogger(SimpleDataSource.class);
	
	
	/*
	 * Constructor por defecto
	 * */
	public SimpleDataSource() {
		super();
	}


	/**
	 * Gets the current conn.
	 * @return  the current conn
	 * @uml.property  name="currentConn"
	 */
	public Connection getCurrentConn() {
		return currentConn;
	}

	
	/**
	 * Gets the opened conns.
	 * @return  the opened conns
	 * @uml.property  name="openedConns"
	 */
	public int getOpenedConns() {
		return openedConns;
	}

	
	/**
	 * Release connection.
	 *
	 * @param conn the conn
	 */
	public void releaseConnection(Connection conn) {
		ConnectionManager.releaseConnection(conn, null);
		openedConns--;
	}
	

	/**
	 * <p>Attempts to establish a connection with the data source that
	 * this <code>DataSource</code> object represents.
	 *
	 * @return  a connection to the data source
	 * @throws SQLException if a database access error occurs
	 */
	public Connection getConnection() throws SQLException {
		try {
			System.out.println(this.getClass().getResource("/").getPath());
			LOG.info(this.getClass().getResource("/").getPath());
			PoolerConnectionSource pooler = new PoolerConnectionSource();
			pooler.initPooler(null);
			Connection conn =  pooler.getConnection(null);
			openedConns++;
			currentConn = conn;
			return conn;
		}
		catch(Throwable t) {
			t.printStackTrace();
			throw new SQLException(t.getMessage());
		}
	}

	/**
	 * <p>Attempts to establish a connection with the data source that
	 * this <code>DataSource</code> object represents.
	 *
	 * @param username the database user on whose behalf the connection is
	 * being made
	 * @param password the user's password
	 * @return  a connection to the data source
	 * @throws SQLException if a database access error occurs
	 * @since 1.4
	 */
	public Connection getConnection(String username, String password) throws SQLException {
		return getConnection();
	}

	/* (non-Javadoc)
	 * @see javax.sql.CommonDataSource#getLogWriter()
	 */
	public PrintWriter getLogWriter() throws SQLException {
		return pw;
	}

	/* (non-Javadoc)
	 * @see javax.sql.CommonDataSource#getLoginTimeout()
	 */
	/**
	 * @return
	 * @throws SQLException
	 * @uml.property  name="loginTimeout"
	 */
	public int getLoginTimeout() throws SQLException {
		return loginTimeout;
	}

	/* (non-Javadoc)
	 * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
	 */
	public void setLogWriter(PrintWriter out) throws SQLException {
		pw = out;
	}

	/* (non-Javadoc)
	 * @see javax.sql.CommonDataSource#setLoginTimeout(int)
	 */
	/**
	 * @param seconds
	 * @throws SQLException
	 * @uml.property  name="loginTimeout"
	 */
	public void setLoginTimeout(int seconds) throws SQLException {
		loginTimeout = seconds;
	}

	/* (non-Javadoc)
	 * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
	 */
	public boolean isWrapperFor(Class iface) throws SQLException {
		return false;
	}

	/* (non-Javadoc)
	 * @see java.sql.Wrapper#unwrap(java.lang.Class)
	 */
	public Object unwrap(Class iface) throws SQLException {
		return null;
	}			

}
